package interfaces;

import java.awt.Paint;
import java.util.HashMap;
import java.util.HashSet;

import org.fenggui.Button;
import org.fenggui.ComboBox;
import org.fenggui.FengGUI;
import org.fenggui.ILabel;
import org.fenggui.IWidget;
import org.fenggui.ScrollBar;
import org.fenggui.ScrollContainer;
import org.fenggui.TextEditor;
import org.fenggui.appearance.TextAppearance;
import org.fenggui.binding.render.Binding;
import org.fenggui.binding.render.ITexture;
import org.fenggui.binding.render.ImageFont;
import org.fenggui.binding.render.text.DirectTextRenderer;
import org.fenggui.binding.render.text.ITextRenderer;
import org.fenggui.event.key.Key;
import org.fenggui.event.mouse.MouseButton;
import org.fenggui.event.mouse.MousePressedEvent;
import org.fenggui.event.mouse.MouseReleasedEvent;
import org.fenggui.text.content.factory.simple.TextStyle;
import org.fenggui.text.content.factory.simple.TextStyleEntry;
import org.fenggui.util.Alphabet;
import org.fenggui.util.Color;
import org.fenggui.util.fonttoolkit.AssemblyLine;
import org.fenggui.util.fonttoolkit.BinaryDilation;
import org.fenggui.util.fonttoolkit.Clear;
import org.fenggui.util.fonttoolkit.DrawCharacter;
import org.fenggui.util.fonttoolkit.FontFactory;
import org.fenggui.util.fonttoolkit.PixelReplacer;

import com.jme.system.DisplaySystem;

public class GUISource {
	
	public static final Color green = new Color(0.05f, 0.6f, 0.05f, 1f);
	public static ImageFont middleFont, bigFont, biggestFont, smallFont, smallestFont;
	
	private static HashMap<String, ITexture> loadedTextures = new HashMap<String, ITexture>();
	
	private static final Alphabet alphabet = Alphabet.GERMAN;
	
	public static void pushButton(Button button) {
		int x = button.getX();
		int y = button.getY();
		MouseButton mb = MouseButton.LEFT;
		button.mousePressed(new MousePressedEvent(button, x, y, mb, 1, new HashSet<Key>()));
		button.mouseReleased(new MouseReleasedEvent(button, x, y, mb, 1, new HashSet<Key>()));
	}
	
	public static ITexture loadTexture(String path) {
		if(loadedTextures.containsKey(path)) return loadedTextures.get(path);
		ITexture texture = null;
		
		try {
			texture = Binding.getInstance().getTexture(path);
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		loadedTextures.put(path, texture);
		return texture;
	}
	
	public static void setTheme(IWidget widget) { FengGUI.getTheme().setUp(widget); }
	
	public static void init() {
		int f = 25;
		int i = 10;
		
		biggestFont = createFont(f);
		f += i;
		bigFont = createFont(f);
		f += i;
		middleFont = createFont(f);
		f += i;
		smallFont = createFont(f);
		f += i;
		smallestFont = createFont(f);
	}
	
	public static ImageFont createFont(int factor) {
		return FontFactory.renderStandardFont(new java.awt.Font("Sans", java.awt.Font.BOLD, 
				DisplaySystem.getDisplaySystem().getHeight() / factor), true, alphabet);
	}
	
	public static ImageFont createBorderFont(int size) {
		java.awt.Font awtFont = new java.awt.Font("Helvetica", java.awt.Font.BOLD, size);
		
		FontFactory ff = new FontFactory(Alphabet.getDefaultAlphabet(), awtFont);
		AssemblyLine line = ff.getAssemblyLine();
		
		Paint blackPaint = new java.awt.Color(0f, 0f, 0f);
		Paint whitePaint = new java.awt.Color(1f, 1f, 1f);
		
		line.addStage(new Clear());
		line.addStage(new DrawCharacter(java.awt.Color.WHITE, true));
		
		line.addStage(new BinaryDilation(java.awt.Color.WHITE, 4));
		
		line.addStage(new PixelReplacer(blackPaint, java.awt.Color.WHITE));
		line.addStage(new DrawCharacter(java.awt.Color.CYAN, true));
		
		line.addStage(new PixelReplacer(whitePaint, java.awt.Color.CYAN));
		
		ImageFont font = ff.createFont();
		
		return font;
	}
	
	public static ScrollContainer createScrollContainer() {
		ScrollContainer scrollContent = new ScrollContainer();
		setTheme(scrollContent);
		ScrollBar horScrollBar = scrollContent.getHorizontalScrollBar();
		ScrollBar verScrollBar = scrollContent.getVerticalScrollBar();
		setTheme(horScrollBar);
		setTheme(verScrollBar);
		
		return scrollContent;
	}
	
	public static ComboBox createComboBox(ImageFont font) {
		ComboBox comboBox = new ComboBox();
		setTheme(comboBox);
		setTheme(comboBox.getPopupContainer());
		setTheme(comboBox.getLabel());
		setTheme(comboBox.getList());
		
		setFont(comboBox, font);
		
		return comboBox;
	}
	
	public static void setFont(TextAppearance textApp, ImageFont font) {
		ITextRenderer renderer = new DirectTextRenderer(font);
		textApp.addRenderer(ITextRenderer.DEFAULTTEXTRENDERERKEY, renderer);
	}
	
	public static void setFont(ComboBox comboBox, ImageFont font) {
		setFont(comboBox.getLabel(), font);
		setFont(comboBox.getList().getAppearance(), font);
	}
	
	public static void setFont(ILabel labelWidget, ImageFont font) {
		setFont(labelWidget.getAppearance(), font);
	}
	
	public static void setFont(TextEditor labelWidget, ImageFont font) {
		setFont(labelWidget.getAppearance(), font);
	}
	
	public static void setTextColor(ILabel labelWidget, Color color) {
		labelWidget.getAppearance().getStyle(TextStyle.DEFAULTSTYLEKEY).
				getTextStyleEntry(TextStyleEntry.DEFAULTSTYLESTATEKEY).setColor(color);
	}
}